package org.hxyjs.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.hxyjs.aop.SystemMail;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.OrderForm;
import org.hxyjs.entity.Vo;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.OrderFormService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 orderFormController
@RequestMapping("/orderFormController") //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /orderFormController
public class OrderFormController {
	@Resource
    OrderFormService orderFormService;
	@RequestMapping("/getAllOrderForm")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<OrderForm> getAllOrderForm() throws Exception{
    	System.out.println("查询全部饭店订单记录");
    	return orderFormService.getAllOrderForms();
    }

    @RequestMapping("/deleteOrderFormByUrno/{urno}")
	@ResponseBody
    public DTO<OrderForm> deleteOrderFormByUrno(@RequestBody @PathVariable("urno") String urno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(urno+"删除饭店订单记录");
    	return orderFormService.deleteOrderFormByUrno(urno);
    }

	@RequestMapping("/addOrderForm")
	@ResponseBody
	public DTO<OrderForm> addOrderForm(@RequestBody OrderForm orderForm,HttpServletResponse response,HttpServletRequest request) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加饭店订单记录");
		Optional<OrderForm> orderFormOp = Optional.ofNullable( orderForm);
		//Optional orderFormOp = Optional.ofNullable(vo.getOrderForm());
		System.out.println(orderForm);
//		return orderFormService.addOrderForm(orderForm);
		//用户号加饭店号加时间加两位自编号  订单总长度为六位饭店号加五位用户号加14位日期加时间加一位自编号
		DTO<OrderForm> dto = new DTO<>();
		if( orderFormOp.isPresent()){
			dto = orderFormService.addOrderForm(orderFormOp.get());
			ObjectMapper objectMapper = new ObjectMapper();
//			Cookie orderFormsCookie = new Cookie("user" + orderForm.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//			//userCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//			orderFormsCookie.setPath("/");
//			orderFormsCookie.setDomain("localhost");
//			orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(orderFormsCookie);
		}else {

			dto = new DTO<>(406,"非法操作");
		}


		return dto;
	}

	@RequestMapping("/updateOrderFormByUrno")
	@ResponseBody
	public DTO<OrderForm> updateOrderFormByUrno(@RequestBody OrderForm orderForm) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改饭店订单记录成功");
		System.out.println(orderForm);
		return orderFormService.updateOrderFormByUrno(orderForm);
	}
	@RequestMapping("/getOrderFormByUrno/{urno}")
	@ResponseBody
	public DTO<OrderForm> getOrderFormByUrno(@PathVariable("urno") String urno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询饭店订单记录");
		return orderFormService.getOrderFormByUrno(urno);
	}
	@RequestMapping("/getOrderFormByPage")
	@ResponseBody
	public DTO<OrderForm> getOrderFormByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		String cacheId = "OrderFormByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		return pageInfo.getCurrPage() > 2 ? orderFormService.getOrderFormByPage(pageInfo) :
				orderFormService.getOrderFormByPage(pageInfo,cacheId);
	}
	@RequestMapping("/processOrder/{urno}/{type}")
	@ResponseBody
	public DTO<OrderForm> processOrder(@PathVariable("urno")String urno,@PathVariable("type") String type) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("处理订单");
		System.out.println(urno);
		//Optional orderFormOp = Optional.ofNullable(vo.getOrderForm());
//		return orderFormService.addOrderForm(orderForm);
		//用户号加饭店号加时间加两位自编号  订单总长度为六位饭店号加五位用户号加14位日期加时间加一位自编号
		return orderFormService.processOrder(urno,type);
	}
	@SystemMail(op = "User:paySuccess",queueName = "family.mail",module = "订单管理",methods = "支付")
	@RequestMapping("/processPay/{urno}")
	@ResponseBody
	public DTO<OrderForm> processPay(@PathVariable("urno")String urno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("处理订单");

		//Optional orderFormOp = Optional.ofNullable(vo.getOrderForm());
//		return orderFormService.addOrderForm(orderForm);
		//用户号加饭店号加时间加两位自编号  订单总长度为六位饭店号加五位用户号加14位日期加时间加一位自编号
		DTO<OrderForm> dto = orderFormService.processPay(urno);
		return  dto;
	}


}
